package com.jack.lib.ui.widget

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.jack.lib.ui.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader


/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/11/20 14:18
 */
open class JImageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : AppCompatImageView(context, attrs, defStyleAttr) {

    private var mJPadding = 0
    private var mJPreView = false
    var imageUrl: String? = null
        private set
    private var mImageResource = 0

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.JImageView)
        parseAttr(typedArray)
        typedArray.recycle()
    }


    private fun parseAttr(typedArray: TypedArray) {
        val drawable = typedArray.getDrawable(R.styleable.JImageView_JSrc)
        mJPreView = typedArray.getBoolean(R.styleable.JImageView_JPreView, false)
        drawable?.let { setImageDrawable(it) }
        val select = typedArray.getBoolean(R.styleable.JImageView_JSelect, false)
        if (select) {
            isSelected = select
        }
        mJPadding = typedArray.getDimension(R.styleable.JImageView_JPadding, 0f).toInt()
        if (mJPreView) {
            setOnClickListener { v: View? ->
                if (!TextUtils.isEmpty(imageUrl)) {
                    XPopup.Builder(context)
                        .asImageViewer(this, imageUrl, SmartGlideImageLoader())
                        .show()
                } else if (mImageResource != 0) {
                    XPopup.Builder(context)
                        .asImageViewer(this, mImageResource, SmartGlideImageLoader())
                        .show()
                }
            }
        }
    }

    override fun setLayoutParams(params: ViewGroup.LayoutParams) {
        if (paddingLeft == 0 && paddingTop == 0 && paddingRight == 0 && paddingBottom == 0 && mJPadding != 0) {
            setPadding(mJPadding, mJPadding, mJPadding, mJPadding)
            params.width += mJPadding * 2
            params.height += mJPadding * 2
        }
        super.setLayoutParams(params)
    }

    override fun setPressed(pressed: Boolean) {
        super.setPressed(pressed)
        val alpha = if (pressed && isClickable) 0.8f else 1f
        if (alpha != getAlpha()) {
            setAlpha(alpha)
        }
    }


    override fun setImageResource(resId: Int) {
        super.setImageResource(resId)
        mImageResource = resId
    }



    fun setGif(@DrawableRes gif: Int) {
        Glide.with(context)
            .load(gif)
            .into(this)
    }

    fun loadImage(url: String?, @DrawableRes placeholder: Int = 0, @DrawableRes error: Int = 0) {
        imageUrl = url
        if (context is Activity && (context as Activity).isDestroyed) {
            return
        }
        val options = RequestOptions()
            .placeholder(placeholder)
            .error(error)
        Glide.with(this)
            .load(url)
            .apply(options)
            .into(this)
    }

    fun loadImage(resId: Int, @DrawableRes placeholder: Int = 0, @DrawableRes error: Int = 0) {
        mImageResource = resId
        if (context is Activity && (context as Activity).isDestroyed) {
            return
        }
        val options = RequestOptions()
            .placeholder(placeholder)
            .error(error)
        Glide.with(this)
            .load(resId)
            .apply(options)
            .into(this)
    }
}